package cn.zhz.customview.view.explosion;

import android.graphics.Point;
import android.graphics.Rect;

import java.util.Random;

/**
 * @author: zhuanghz
 * @createdTime: 2019/6/5 13:05
 * @desc:
 */
public class Particle {

    public static final int PART_WH = 10;//小球的默认半径，可修改
    public float cx, cy;//粒子的中心坐标
    public int color;//颜色
    public float alpha;//透明度
    public float radius;
    private Random random = new Random();
    private Rect mBound;

    private Particle() {
    }


    /**
     * 创建粒子
     */
    public static Particle createParticle(int color, Rect bound, Point point) {
        int row = point.y;
        int column = point.x;
        Particle particle = new Particle();
        particle.mBound = bound;
        particle.color = color;
        particle.alpha = 1f;
        particle.radius = PART_WH;
        particle.cx = bound.left + PART_WH * (column + 1f);
        particle.cy = bound.top + PART_WH * (row + 1f);
        return particle;
    }

    /**
     * 粒子坠落
     */
    public void dispersal(float offset) {
        cx = cx + offset * random.nextInt(mBound.width()) * (random.nextFloat() - 0.5f);
        cy = cy + offset * random.nextInt(mBound.height() /2);
        radius = radius - offset * random.nextInt(2);
        alpha = (1f - offset) * (1 + random.nextFloat());
    }

    /**
     * 测试粒子爆炸
     * */
    public void explosion(float offset){
        radius = radius - offset * random.nextInt(2);
        alpha = (1f - offset) * (1 + random.nextFloat());
        cx = cx + offset * random.nextInt(mBound.width()*2) * (random.nextFloat() - 0.5f);

        //Y轴先向上，在往下执行
        if(offset<0.25f){
            cy=cy-offset * random.nextInt(mBound.height());
        }else{
            cy=cy+offset * random.nextInt(mBound.height());
        }
    }



}
